home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / StringReader.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  158 lines

  1. /*
  2.  * @(#)StringReader.java    1.6 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * A character stream whose source is a string.
  20.  *
  21.  * @version     1.6, 98/07/01
  22.  * @author    Mark Reinhold
  23.  * @since    JDK1.1
  24.  */
  25.  
  26. public class StringReader extends Reader {
  27.  
  28.     private String str;
  29.     private int length;
  30.     private int next = 0;
  31.     private int mark = 0;
  32.  
  33.     /**
  34.      * Create a new string reader.
  35.      */
  36.     public StringReader(String s) {
  37.     this.str = s;
  38.     this.length = s.length();
  39.     }
  40.  
  41.     /** Check to make sure that the stream has not been closed */
  42.     private void ensureOpen() throws IOException {
  43.     if (str == null)
  44.         throw new IOException("Stream closed");
  45.     }
  46.  
  47.     /**
  48.      * Read a single character.
  49.      *
  50.      * @return     The character read, or -1 if the end of the stream has been
  51.      *             reached
  52.      *
  53.      * @exception  IOException  If an I/O error occurs
  54.      */
  55.     public int read() throws IOException {
  56.     synchronized (lock) {
  57.         ensureOpen();
  58.         if (next >= length)
  59.         return -1;
  60.         return str.charAt(next++);
  61.     }
  62.     }
  63.  
  64.     /**
  65.      * Read characters into a portion of an array.
  66.      *
  67.      * @param      cbuf  Destination buffer
  68.      * @param      off   Offset at which to start writing characters
  69.      * @param      len   Maximum number of characters to read
  70.      *
  71.      * @return     The number of characters read, or -1 if the end of the
  72.      *             stream has been reached
  73.      *
  74.      * @exception  IOException  If an I/O error occurs
  75.      */
  76.     public int read(char cbuf[], int off, int len) throws IOException {
  77.     synchronized (lock) {
  78.         ensureOpen();
  79.         if (next >= length)
  80.         return -1;
  81.         int n = Math.min(length - next, len);
  82.         str.getChars(next, next + n, cbuf, off);
  83.         next += n;
  84.         return n;
  85.     }
  86.     }
  87.  
  88.     /**
  89.      * Skip characters.
  90.      *
  91.      * @exception  IOException  If an I/O error occurs
  92.      */
  93.     public long skip(long ns) throws IOException {
  94.     synchronized (lock) {
  95.         ensureOpen();
  96.         if (next >= length)
  97.         return 0;
  98.         long n = Math.min(length - next, ns);
  99.         next += n;
  100.         return n;
  101.     }
  102.     }
  103.  
  104.     /**
  105.      * Tell whether this stream is ready to be read.  String readers are
  106.      * always ready to be read.
  107.      */
  108.     public boolean ready() {
  109.     return true;
  110.     }
  111.  
  112.     /**
  113.      * Tell whether this stream supports the mark() operation, which it does.
  114.      */
  115.     public boolean markSupported() {
  116.     return true;
  117.     }
  118.  
  119.     /**
  120.      * Mark the present position in the stream.  Subsequent calls to reset()
  121.      * will reposition the stream to this point.
  122.      *
  123.      * @param  readAheadLimit  Limit on the number of characters that may be
  124.      *                         read while still preserving the mark.  Because
  125.      *                         the stream's input comes from a string, there
  126.      *                         is no actual limit, so this argument is ignored.
  127.      *
  128.      * @exception  IOException  If an I/O error occurs
  129.      */
  130.     public void mark(int readAheadLimit) throws IOException {
  131.     synchronized (lock) {
  132.         ensureOpen();
  133.         mark = next;
  134.     }
  135.     }
  136.  
  137.     /**
  138.      * Reset the stream to the most recent mark, or to the beginning of the
  139.      * string if it has never been marked.
  140.      *
  141.      * @exception  IOException  If an I/O error occurs
  142.      */
  143.     public void reset() throws IOException {
  144.     synchronized (lock) {
  145.         ensureOpen();
  146.         next = mark;
  147.     }
  148.     }
  149.  
  150.     /**
  151.      * Close the stream.
  152.      */
  153.     public void close() {
  154.     str = null;
  155.     }
  156.  
  157. }
  158.